package labelsystem.Service.Implements;/**
 * @projectName careOlder
 * @Description TODO
 * @author MCG
 * @time 2023/3/7 17:00
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import labelsystem.Mapper.*;
import labelsystem.Pojo.*;
import labelsystem.Service.yuserService;
import labelsystem.utils.monitorHandle;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

/**
 *description: TODO
 *@author MCG
 *@date 2023/3/7 17:00
 *@Version 1.0
 */
@Service
public class yuserServiceImplement implements yuserService {
    @Resource
    yuserMapper yuserMapper;
    @Resource
    ouserMapper ouserMapper;
    @Resource
    relationMapper relationMapper;
    @Resource
    orderMapper orderMapper;
    @Resource
    monitorMapper monitorMapper;
    @Resource
    doctorMapper doctorMapper;
    @Resource
    deviceMapper deviceMapper;
    @Resource
    checkMapper checkMapper;
    @Resource
    volunteerMapper volunteerMapper;
    @Override
    public boolean loginInByPassword(String telephone, String password) {
        QueryWrapper<YoungUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone",telephone);
        List<YoungUser> users=yuserMapper.selectList(queryWrapper);
        return password.equals(users.get(0).getPassword());
    }

    @Override
    public boolean loginByVerifyCode(String input, String code) {
        return input.equals(code);
    }

    @Override
    public YoungUser register(String name, String telephone, String password, String gender) {
        YoungUser youngUser=new YoungUser();
        youngUser.setName(name);
        youngUser.setGender(gender);
        youngUser.setPhone(Long.parseLong(telephone));
        youngUser.setPassword(password);
        yuserMapper.insert(youngUser);
        return youngUser;
    }



    @Override
    public boolean registerOlder(Long yID, String name, String address, int age, String gender, String status, Long phone, String relationship) {
       //插入老人信息
        OlderUser olderUser=new OlderUser();
        olderUser.setAge(age);
        olderUser.setPhone(phone);
        olderUser.setAddress(address);
        olderUser.setGender(gender);
        olderUser.setStatus(status);
        olderUser.setName(name);
        ouserMapper.insert(olderUser);
        QueryWrapper<OlderUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name).eq("phone" ,phone);
        //查询老人id
        Long oId=ouserMapper.selectOne(queryWrapper).getId();
        //插入关系列表
        Mapoy mapoy=new Mapoy();
        mapoy.setOId(oId);
        mapoy.setYId(yID);
        mapoy.setRelation(relationship);
        relationMapper.insert(mapoy);
        return true;
    }


    @Override
    public List<Order> queryOOrders(Long yId) {
        List<Order>  res=new LinkedList<>();
        //根据yid查询oId
        List<Mapoy> olds=relationMapper.selectList(new QueryWrapper<Mapoy>().eq("y_id",yId));
        for(Mapoy temp:olds){
            Long oID=temp.getYId();
            QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("accept_id",oID);
            List <Order> orders=orderMapper.selectList(queryWrapper);
            res.addAll(orders);
        }
        return res;
    }

    @Override
    public List<Doctor> queryDoctors() {
        return doctorMapper.selectList(null) ;
    }

    @Override
    public List<Doctor> queryDoctorBySpecify(String specify) {
        QueryWrapper<Doctor> doctorQueryWrapper=new QueryWrapper<>();
        doctorQueryWrapper.eq("specify",specify);
        return doctorMapper.selectList(doctorQueryWrapper);
    }

    @Override
    public Doctor queryDoctorById(Long doctorID) {
        return doctorMapper.selectById(doctorID);
    }

    @Override
    public List<Check> queryCheckById(Long yID) {
        List<Check> res=new LinkedList<>();
        QueryWrapper<Mapoy> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("y_id",yID);
        List<Mapoy> list=relationMapper.selectList(queryWrapper);
        for(Mapoy temp:list){
           Long id=temp.getOId();
           QueryWrapper<Check> checkQueryWrapper=new QueryWrapper<>();
           checkQueryWrapper.eq("bindId",id);
           res.addAll(checkMapper.selectList(checkQueryWrapper));
        }
        return res;
    }

    @Override
    public List<monitorHandle> queryMonitor(Long yID) {
        List<monitorHandle> res=new LinkedList<>();
        QueryWrapper<Mapoy> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("y_id",yID);
        List<Mapoy> list=relationMapper.selectList(queryWrapper);
        for(Mapoy temp:list){
            Long id=temp.getOId();
            QueryWrapper<Monitor> monitorQueryWrapper=new QueryWrapper<>();
            monitorQueryWrapper.eq("id",id);
             Monitor monitor=monitorMapper.selectOne(monitorQueryWrapper);
             if(monitor!=null){
                 monitorHandle monitorHandle=new monitorHandle(monitor);
                 res.add(monitorHandle);
             }else{
                 continue;
             }

        }
        return res;
    }

    @Override
    public List<Device> queryDeviceCheck(Long yID) {
        List<Device> devices=new LinkedList<>();
        QueryWrapper<Mapoy> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("y_id",yID);
        List<Mapoy> lists=relationMapper.selectList(queryWrapper);
        for(Mapoy temp :lists){
            Long id=temp.getOId();
            devices.addAll(deviceMapper.selectList(new QueryWrapper<Device>().eq("bindId",id)));
        }
        return devices;
    }

    @Override
    public List<Volunteer> queryVolunteers() {
        return volunteerMapper.selectList(null);
    }

    @Override
    public Volunteer queryServiceById(Long vID) {
        return volunteerMapper.selectById(vID);
    }


    @Override
    public boolean AddMonitor(String name,String phone,String medicine,int cycle) {
        QueryWrapper<OlderUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name).eq("phone",Long.parseLong(phone));
        OlderUser olderUser=ouserMapper.selectOne(queryWrapper);


        Long id=olderUser.getId();
        //插入检测表
        Monitor monitor=monitorMapper.selectById(id);
        if(monitor==null){
            monitor.setId(olderUser.getId());
            monitor.setMedicine(medicine);
            String bitmap=new String();
            for(int i=0;i<cycle;i++){
                bitmap+="0";
            }
            monitor.setIsUsed(bitmap);
            monitorMapper.insert((monitor));
        }else{
            LocalDateTime startTime=monitor.getCreateTime();
            LocalDateTime endTime=LocalDateTime.now();
            Duration duration=Duration.between(startTime,endTime);
            if(duration.toDays()>cycle){
                monitorMapper.deleteById(id);
                monitor.setId(id);
                monitor.setMedicine(medicine);
                String bitmap=new String();
                for(int i=0;i<cycle;i++){
                    bitmap+="0";
                }
                monitor.setIsUsed(bitmap);
                monitorMapper.insert((monitor));
            }else{
                return false;
            }
        }
        return true;
    }

    @Override
    public YoungUser queryInfoBytel(String phone) {
        QueryWrapper<YoungUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone",Long.parseLong(phone));
        return yuserMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean updateInfo(YoungUser youngUser) {
        return 1==yuserMapper.updateById(youngUser);
    }

    @Override
    public boolean deleteMonitor(Long oid) {
        return 1==monitorMapper.deleteById(oid);
    }
}
